package com.lam.system.service.impl;

import java.util.List;
import java.util.Objects;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lam.common.components.dict.DictCacheUtils;
import com.lam.common.exception.ParameterErrorException;
import com.lam.common.utils.StringUtils;
import com.lam.framework.bean.entity.SysDict;
import com.lam.framework.bean.entity.SysDictItem;
import com.lam.system.mapper.SysDictItemMapper;
import com.lam.system.param.SysDictItemParam;
import com.lam.system.service.SysDictItemService;
import com.lam.system.service.SysDictService;

/**
 * 字典项Service业务层处理
 * 
 * @author admin
 * @date 2022-09-19
 */
@Service
public class SysDictItemServiceImpl implements SysDictItemService {

    @Autowired
    private SysDictItemMapper sysDictItemMapper;
    @Autowired
    private SysDictService sysDictService;

    /**
     * 查询字典项
     * 
     * @param id ID
     * @return 单个对象
     */
    @Override
    @Transactional(readOnly = true)
    public SysDictItem selectById(String id) {

        return sysDictItemMapper.selectById(id);
    }

    /**
     * 查询字典项列表
     * 
     * @param param
     * @return 
     */
    @Override
    @Transactional(readOnly = true)
    public List<SysDictItem> selectList(SysDictItemParam param) {
    
        QueryWrapper<SysDictItem> queryWrapper = new QueryWrapper<>();
        
        //关键字查询
        if(StringUtils.isNotBlank(param.getKeywords())) {
        	queryWrapper.and(wr->{
        		wr.or().like("item_value", param.getKeywords());
        		wr.or().like("item_text", param.getKeywords());
        	});
        }
        
        queryWrapper.eq(StringUtils.isNotBlank(param.getDictId()), "dict_id", param.getDictId());
        queryWrapper.like(StringUtils.isNotBlank(param.getItemValueKey()), "item_value", param.getItemValueKey());
        queryWrapper.eq(StringUtils.isNotBlank(param.getItemValue()), "item_value", param.getItemValue());
        queryWrapper.eq(Objects.nonNull(param.getStatus()), "status", param.getStatus());

        return sysDictItemMapper.selectList(queryWrapper);
    }

    /**
     * 新增字典项
     * 
     * @param sysDictItem 
     * @return 
     */
    @Override
    @Transactional
    public int insert(SysDictItem sysDictItem) {
    
    	SysDict sysDict = sysDictService.selectById(sysDictItem.getDictId());
    	if(sysDict == null) {
    		throw new ParameterErrorException("dictId不存在！");
    	}
        int n = sysDictItemMapper.insert(sysDictItem);
    	DictCacheUtils.delDictCache(sysDict.getDictCode());
        return n;
    }

    /**
     * 修改字典项
     * 
     * @param sysDictItem 
     * @return 
     */
    @Override
    @Transactional
    public int update(SysDictItem sysDictItem) {
    
    	SysDict sysDict = sysDictService.selectById(sysDictItem.getDictId());
    	if(sysDict == null) {
    		throw new ParameterErrorException("dictId不存在！");
    	}
        int n = sysDictItemMapper.updateById(sysDictItem);
    	DictCacheUtils.delDictCache(sysDict.getDictCode());
        return n;
    }

    /**
     * 删除字典项信息
     * 
     * @param idStr ID，多个用逗号分隔
     * @return 
     */
    @Override
    @Transactional
    public int deleteById(String idStr) {
    	if(StringUtils.isBlank(idStr)) {
            return 0;
    	}
    	List<String> ids = StringUtils.str2List(idStr);
    	SysDictItem sysDictItem = selectById(ids.get(0));
    	int n = sysDictItemMapper.deleteBatchIds(ids);
    	
    	if(n > 0 && sysDictItem != null) {
    		SysDict sysDict = sysDictService.selectById(sysDictItem.getDictId());
        	if(sysDict != null) {
        		DictCacheUtils.delDictCache(sysDict.getDictCode());
        	}
    	}
    	
        return n;
    }
    
}
